﻿using HAF.FtpUtility;
using HAF.LogUtility;
using HAF.LogUtility.Assistantor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoapNotify
{
    public partial class FrmUpload : Form
    {
        public FrmUpload()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                double iOut = 20 % 10;
                iOut = 28 % 10;
                iOut = 38 % 10;
                iOut = 50 % 10;
                iOut = 58 % 10;
                iOut = 100 % 10;

                var tmpSiteParams = FtpUrlParser.ParseFtpUrl(txtRemoteUrl.Text);
                if (tmpSiteParams != null)
                {
                    ShowMessage(string.Format("开始上传文件{0}", txtRemoteUrl.Text));
                    //启动下载
                    using (FluentFtpHelper ftpClien = new FluentFtpHelper(tmpSiteParams))
                    {
                        ftpClien.LogOutName = TimeHelper.GetTimeStamp();
                        ftpClien.WriteConsoleMessageEvent = ShowMessage;
                        //ftpClien.ReportMessageEvent = PushLog;
                        //ftpClien.UploadEndEvent = TaskEnd;

                        //注意：Path.Combine使用时，后续的地址不能加/或者//
                        //string localPath = Path.Combine(_runConfig.SaveDir, dateStr, cpName);
                        ftpClien.UploadFile(txtLocalFileAddress.Text, tmpSiteParams.FileSets[0].RemoteRootDirectory, ftpClien.LogOutName);

                    };
                }

            }
            catch (Exception ex)
            {
                ShowPopTips(ex.InnerException.ToString());
                LogHelper.WriteErrorLog(ex.ToString());
            }
        }

        private void TaskEnd(string tid, bool stat)
        {
            LogHelper.WriteInfoLog("任务完成 {0}", stat);
        }

        #region 消息输出
        private void ShowPopTips(string message)
        {
            MessageBox.Show(message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        int _msgLen = 0;

        private void ShowClear()
        {
            txtMsg.Clear();
        }

        private void ShowMessage(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ShowMessage), msg);
            }
            else
            {
                if (_msgLen >= 300)
                {
                    txtMsg.Clear();
                    //txtMessages.Text = "";
                    _msgLen = 0;
                }
                LogHelper.WriteDebugLogEx("SysMonitor", msg);

                txtMsg.Text = txtMsg.Text + (DateTime.Now.ToString() + " " + msg + "\r\n");
                txtMsg.SelectionStart = txtMsg.Text.Length;
                txtMsg.ScrollToCaret();

                _msgLen++;

            }
        }

        #endregion 消息输出
    }
}
